<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        /**
         * JSON对象
         */
        var user = {
            "id":1001,
            "name":"tom",
            "age":28,
            "height":180.4,
            "address":{
                "province":"江苏省",
                "city":"南京市",
                "district":"秦淮区"
            },
            "isMarried":true
        }

        // 访问对象：对象名.属性名
        console.log(user.name);
        console.log(user.height);
        console.log(user.address.city);
        console.log(user.isMarried);

        /**
         * JSON对象的集合
         */
        var users = [
            {
                "id":9527,
                "username":"zhangsan",
                "password":"111"
            },
            {
                "id":3306,
                "username":"lisi",
                "password":"222"
            },
            {
                "id":1433,
                "username":"wangwu",
                "password":"123"
            }
        ]

        for (var u of users){
            console.log(u.id, u.username, u.password);
        }


        /**
         * JSON和字符串之间的转换
         */
        // 1.将JSON对象转换为字符串
        var person={
            "name":"汤小洋",
            "age":18,
            "height":180.5
        };
        console.log(person);
        console.log(typeof person);

        var str = JSON.stringify(person)
        console.log(str);
        console.log(typeof str);

        // 2.将字符串转换为JSON对象
        var s='{"name":"tom","age":20}';
        console.log(s);
        console.log(typeof s);

        var obj = JSON.parse(s);
        console.log(obj);
        console.log(typeof obj);

    </script>
</head>
<body>

</body>
</html>